﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Data;
using Motion.MotionPlatform.Tools;

namespace Motion.MotionPlatform
{
	public class Positions
	{
		/// <summary>
		/// 记录物体位置
		/// </summary>
		/// <param name="str">物体在json中存储位置信息的表名字</param>
		/// <param name="type">类型：C,Z</param>
		/// <param name="listTrans">存储的物体名</param>
		public static void recordLocations(string str, string type, List<Transform> listTrans)
		{
			//位置信息路径
			string locationPath = MotionPlatformData.taskRootPath + MotionPlatformData.taskName + "/";

			//检验根目录是否存在
			if (Directory.Exists(locationPath))
			{

				ArrayList my_choose2 = new ArrayList();

				for (int i = 0; i < listTrans.Count; i++)
				{
					TransformRecursion(listTrans[i], my_choose2);
				}

				JsonOperator jsonOp = new JsonOperator();
				string[] nameArray = new string[] { "NAME", "POSITION", "EULERANGLES" };
				string[,] contentArray = new string[3, my_choose2.Count];
				string objName = "";
				string objPosition = "";
				string objRotation = "";
				Transform objTransform = null;
				for (int i = 0; i < my_choose2.Count; i++)
				{
					try
					{
						objTransform = ((GameObject)my_choose2[i]).transform;
					}
					catch
					{
						UnityEngine.Debug.LogError(((GameObject)my_choose2[i]).gameObject.name + "，该物体在当前场景中不存在，请确认！");
						return;
					}
					objName = objTransform.name;
					objPosition = objTransform.position.x.ToString() + "," + objTransform.position.y.ToString() +
						"," + objTransform.position.z.ToString();
					objRotation = objTransform.eulerAngles.x.ToString() + "," + objTransform.eulerAngles.y.ToString() +
						"," + objTransform.eulerAngles.z.ToString();
					contentArray[0, i] = objName;
					contentArray[1, i] = objPosition;
					contentArray[2, i] = objRotation;
				}
				jsonOp.JsonWriter(locationPath + type + "/" + "tmpLocation.json", str, nameArray, contentArray, false);
			}
		}

		public static void TransformRecursion(Transform aimTrans, ArrayList al)
		{
			al.Add(aimTrans.gameObject);
			if (aimTrans.childCount > 0)
			{
				foreach (Transform child in aimTrans)
				{
					TransformRecursion(child, al);
				}
			}
		}

		public static void recordLocations(string str, string type)
		{
			//位置信息路径
			string locationPath = MotionPlatformData.taskRootPath + MotionPlatformData.taskName + "/";

			//检验根目录是否存在
			if (Directory.Exists(locationPath))
			{
				//Json处理类
				JsonOperator jsonOp = new JsonOperator();
				ExcelOperator excelOp = new ExcelOperator();
				if (File.Exists(locationPath + "ObjectName.xls"))
				{
					//首先处理ObjectName文件
					DataTable objNameTab = excelOp.ExcelReader(locationPath + "ObjectName.xls", "NAME", "A", "A");
					//为Json写入做准备
					string[] nameArray = new string[] { "NAME", "POSITION", "EULERANGLES" };
					string[,] contentArray = new string[3, objNameTab.Rows.Count];
					string objName = "";
					string objPosition = "";
					string objRotation = "";
					Transform objTransform = null;
					for (int i = 0; i < objNameTab.Rows.Count; i++)
					{
						try
						{
							objTransform = GameObject.Find((string)objNameTab.Rows[i][0].ToString()).transform;
						}
						catch
						{
							UnityEngine.Debug.LogError((string)objNameTab.Rows[i][0].ToString() + "，该物体在当前场景中不存在，请确认！");
							return;
						}
						objName = objTransform.name;
						objPosition = objTransform.position.x.ToString() + "," + objTransform.position.y.ToString() +
							"," + objTransform.position.z.ToString();
						objRotation = objTransform.eulerAngles.x.ToString() + "," + objTransform.eulerAngles.y.ToString() +
							"," + objTransform.eulerAngles.z.ToString();
						contentArray[0, i] = objName;
						contentArray[1, i] = objPosition;
						contentArray[2, i] = objRotation;
					}
					jsonOp.JsonWriter(locationPath + type + "/" + "tmpLocation.json", str, nameArray, contentArray, false);
				}
				else
				{
					UnityEngine.Debug.LogError(locationPath + "ObjectName.xls，该文件不存在，请确认！");
					return;
				}
			}
			else
			{
				UnityEngine.Debug.LogError(locationPath + "，该路径不存在，请检查！");
				return;
			}
		}

		/// <summary>
		/// 设置物体位置
		/// </summary>
		/// <param name="str">物体位置在json文件中存储的表名字</param>
		/// <param name="list">设置那些物体的位置</param>
		/// <param name="isAll">是否将json文件中存储的所有物体全部还原</param>
		public static void setLocations(string str, List<string> list, bool isAll)
		{
			JsonOperator jsonOp = new JsonOperator();
			//位置信息路径
			string locationPath = MotionPlatformData.taskRootPath + MotionPlatformData.taskName + "/";

			if (!File.Exists(locationPath + "C/" + "tmpLocation.json"))
			{
				Debug.LogError(locationPath + "C/" + "tmpLocation.json文件不存在");
				return;
			}

			DataTable dt = jsonOp.JsonReader(locationPath + "C/" + "tmpLocation.json", str);
			if (dt == null)
			{
				Debug.LogError(locationPath + "C/" + "tmpLocation.json文件中不存在" + str);
				return;

			}

			for (int i = 0, j = 0; i < dt.Rows.Count; i++)
			{
				DataRow dr = dt.Rows[i];

				bool isSelected = true;
				if (isAll == false)
				{
					isSelected = list.Contains(dr[0].ToString());
				}

				if (isSelected)
				{
					Transform objTransform = null;
					bool isRight = true;
					try
					{
						objTransform = GameObject.Find(dr[0].ToString()).transform;
					}
					catch
					{
						UnityEngine.Debug.LogError(dr[0].ToString() + "，该物体在当前场景中不存在，请确认！");
						return;
					}
					objTransform.position = Motion_Compute.Vector3Conversion(dr[1].ToString(), ref isRight);
					if (!isRight)
					{
						UnityEngine.Debug.LogError(dr[1].ToString() + "，位置向量转化错误，请检查！");
						return;
					}
					objTransform.eulerAngles = Motion_Compute.Vector3Conversion(dr[2].ToString(), ref isRight);
					//Debug.Log("setLocations : " + j + "," + objTransform.eulerAngles);
					if (!isRight)
					{
						UnityEngine.Debug.LogError(dr[2].ToString() + "，角度向量转化错误，请检查！");
						return;
					}

					j++;
				}
			}
		}
	}
}
